package me.spring.cloud.common.components.ips.common;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import java.util.stream.Collectors;

public class ReadGlobalRegion {

  /**
   * 获取省市区信息
   *
   * @param filePath
   * @return
   * @throws IOException
   */
  public static void readRegion(String filePath, HashMap<String, Integer> globalRegionMap,
      int mapIndex) throws IOException {
    readRegion(new File(filePath), globalRegionMap, mapIndex);
  }



  public static void readRegionToMap(File file, Map<Integer,
      Map<String, String[]>> groupRegionMap, Map<String, String[]> globalRegionMap)
      throws IOException {
    BufferedReader greader = new BufferedReader(new FileReader(file));
    String gline = null;
    int level = 1;
    Map<String,String[]> tmp = new HashMap<>();
    while ((gline = greader.readLine()) != null) {
      String[] p = gline.split(",");
      if (p.length != 5) {
        continue;
      }
      if(Integer.valueOf(p[3]) != level){
        groupRegionMap.put(level,tmp);
        if(groupRegionMap.containsKey(Integer.valueOf(p[3]))){
          tmp = groupRegionMap.get(Integer.valueOf(p[3]));
        } else {
          tmp = new HashMap<>();
        }
        level = Integer.valueOf(p[3]);
      }
      tmp.put(p[2],p);
      globalRegionMap.put(p[0],p);
    }
    greader.close();
  }

  public static Map<String,Map> transMapToTreeMap(Map<String, String[]> globalRegionMap){
    Map<String,Map> treeMap = new HashMap<>();
    globalRegionMap.forEach((key,value)->{
      //国家
      if(Integer.valueOf(value[3]) == 1){
        if(!treeMap.containsKey(value[2])){
          Map<String,Map> tmp = new HashMap();
          tmp.put(value[1],new HashMap());
          treeMap.put(value[2],tmp);
        }
      } else {
        //TODO 获取当前值的所对应的国家、省、市、区
        if(Integer.valueOf(value[3]) == 2){
          String[] l1 = getLevel(globalRegionMap,Integer.valueOf(value[1]),1);
          Map<String,Map> tmp1 = treeMap.get(l1[2]);
          if(null != tmp1){
            if(!tmp1.containsKey(value[3])){
                tmp1.put(value[3],new HashMap());
                treeMap.remove(l1[2]);
                treeMap.put(l1[2],tmp1);
            }
          }
        } else  if(Integer.valueOf(value[3]) == 3){
            //TODO 获取国家map
            String[] l1 = getLevel(globalRegionMap,Integer.valueOf(value[1]),1);
            Map<String,Map> tmp1 = treeMap.get(l1[2]);
            if(null != tmp1){
              //TODO 获取省map
              String[] l2 = getLevel(globalRegionMap,Integer.valueOf(value[1]),2);
              Map<String,Map> tmp2 = treeMap.get(l2[2]);
              if(null != tmp2){
                if(!tmp2.containsKey(value[3])){
                  tmp2.put(value[3],new HashMap());
                  tmp1.remove(l2[2]);
                  tmp1.put(l2[2],tmp2);
                  treeMap.remove(l1[2]);
                  treeMap.put(l1[2],tmp1);
                }
              }
              //TODO 获取市map
            }
        } else  if(Integer.valueOf(value[3]) == 4){
          //TODO 获取国家map
          String[] l1 = getLevel(globalRegionMap,Integer.valueOf(value[1]),1);
          Map<String,Map> tmp1 = treeMap.get(l1[2]);
          if(null != tmp1){
            //TODO 获取省map
            String[] l2 = getLevel(globalRegionMap,Integer.valueOf(value[1]),2);
            Map<String,Map> tmp2 = treeMap.get(l2[2]);
            if(null != tmp2){
              //TODO 获取市map
              String[] l3 = getLevel(globalRegionMap,Integer.valueOf(value[1]),3);
              Map<String,Map> tmp3 = treeMap.get(l3[2]);
              if(null != tmp3){
                if(!tmp3.containsKey(value[3])){
                  tmp3.put(value[3],new HashMap());
                  tmp2.remove(l3[2]);
                  tmp2.put(l3[2],tmp3);
                  tmp1.remove(l2[2]);
                  tmp1.put(l2[2],tmp2);
                  treeMap.remove(l1[2]);
                  treeMap.put(l1[2],tmp1);
                }
              }
            }
          }
        }
      }
    });
    return treeMap;
  }

  private static String[] getLevel(Map<String, String[]> globalRegionMap,int nextIndex,int level){
    String[] data = null;
    while (nextIndex > 0){
        data = globalRegionMap.get(String.valueOf(nextIndex));
        if(Integer.valueOf(data[3]) != level){
          nextIndex = Integer.valueOf(data[1]);
          data = null;
          continue;
        } else{
          break;
        }
    }
    return data;
  }


  /**
   * 获取省市区信息
   * @param file
   * @param globalRegionMap
   * @param mapIndex
   * @throws IOException
   */
  public static void readRegion(File file, HashMap<String, Integer> globalRegionMap, int mapIndex)
      throws IOException {
    BufferedReader greader = new BufferedReader(new FileReader(file));

    String gline = null;
    while ((gline = greader.readLine()) != null) {
      String[] p = gline.split(",");
      if (p.length != 5) {
        continue;
      }
      //push the mapping
      globalRegionMap.put(p[2], Integer.valueOf(p[mapIndex]));
    }
    greader.close();
  }


  public static String[] readRegionFor(Map<String, Map> regionMap,String str){
    if (null == regionMap || regionMap.isEmpty()){
      return null;
    }
    //TODO 循环第一层，无，则循环第二层
    while (1==1){
    }
  }


}
